/**
 * @className: ShiftService
 * @author: Csh
 * @date: 2024/4/8 14:22
 * @Version: 1.0
 * @description:
 */
package com.zhuojy.equity.mall.admin.service.AttendanceServices;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.bo.CommuteScheduleBO;
import com.zhuojy.equity.mall.admin.common.entity.bo.ShiftBO;
import com.zhuojy.equity.mall.admin.common.entity.po.CommuteSchedulePO;
import com.zhuojy.equity.mall.admin.common.entity.po.ShiftPO;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.CommuteScheduleMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.AttendanceMappers.ShiftMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class ShiftService extends ServiceImpl<ShiftMapper, ShiftPO> {

    @Autowired
    private CommuteScheduleMapper commuteScheduleMapper;


    public ResultEntity findAll(ShiftBO shiftBO, Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<ShiftPO> lqw = Wrappers.lambdaQuery(ShiftPO.class);
        if(shiftBO.getStartTime()!=null){
            lqw.ge(ShiftPO::getStartTime,shiftBO.getStartTime());
        }
        if(shiftBO.getEndTime()!=null){
            lqw.le(ShiftPO::getStartTime,shiftBO.getEndTime());
        }
        if(StringUtils.isNotEmpty(shiftBO.getShiftName())){
            lqw.like(ShiftPO::getShiftName,shiftBO.getShiftName());
        }

        IPage<ShiftPO> iPage = this.baseMapper.selectPage(page, lqw);
        for (ShiftPO record : iPage.getRecords()) {
            record.setCommuteSchedulePO(commuteScheduleMapper.selectOne(Wrappers.lambdaQuery(CommuteSchedulePO.class).eq(CommuteSchedulePO::getId,record.getCommuteTime())));
        }

        Map map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("rows",iPage.getRecords());
        return ResultEntity.OK(map);
    }

    public int addOrUpShift(ShiftBO shiftBO) {
        if(shiftBO.getId()!=null){
            for (String id : shiftBO.getCommuteTime().split(",")) {
                commuteScheduleMapper.deleteById(id);
            }

            String ids = "";
            for (CommuteScheduleBO commuteScheduleBO : shiftBO.getCommuteScheduleBO()) {
                CommuteSchedulePO commuteSchedulePO = new CommuteSchedulePO(null, commuteScheduleBO.getStartDay(), commuteScheduleBO.getStartTime(), commuteScheduleBO.getStartCart(), commuteScheduleBO.getEndDay(), commuteScheduleBO.getEndTime(), commuteScheduleBO.getEndCart(), commuteScheduleBO.getMaxInAdvance(), commuteScheduleBO.getMaxLaterLate(), commuteScheduleBO.getMaxLaterDay(), commuteScheduleBO.getMinInAdvance(), commuteScheduleBO.getMinLaterLate(), commuteScheduleBO.getMinLaterDay());
                commuteScheduleMapper.insert(commuteSchedulePO);
                ids += commuteSchedulePO.getId()+",";
            }
            shiftBO.setCommuteTime(ids);
            this.baseMapper.updateById(new ShiftPO(shiftBO.getId(),shiftBO.getShiftName(),shiftBO.getShiftHead(),shiftBO.getCommuteTime(),shiftBO.getShiftLate(),shiftBO.getShiftLateEnd(),shiftBO.getShiftBoolean(),shiftBO.getShiftMaxLate(),new Date(),shiftBO.getStartBy(),new Date(),null,null));

        }else{
            String ids = "";
            for (CommuteScheduleBO commuteScheduleBO : shiftBO.getCommuteScheduleBO()) {
                CommuteSchedulePO commuteSchedulePO = new CommuteSchedulePO(null, commuteScheduleBO.getStartDay(), commuteScheduleBO.getStartTime(), commuteScheduleBO.getStartCart(), commuteScheduleBO.getEndDay(), commuteScheduleBO.getEndTime(), commuteScheduleBO.getEndCart(), commuteScheduleBO.getMaxInAdvance(), commuteScheduleBO.getMaxLaterLate(), commuteScheduleBO.getMaxLaterDay(), commuteScheduleBO.getMinInAdvance(), commuteScheduleBO.getMinLaterLate(), commuteScheduleBO.getMinLaterDay());
                commuteScheduleMapper.insert(commuteSchedulePO);
                ids += commuteSchedulePO.getId()+",";
            }
            shiftBO.setCommuteTime(ids);
            this.baseMapper.insert(new ShiftPO(null,shiftBO.getShiftName(),shiftBO.getShiftHead(),shiftBO.getCommuteTime(),shiftBO.getShiftLate(),shiftBO.getShiftLateEnd(),shiftBO.getShiftBoolean(),shiftBO.getShiftMaxLate(),new Date(),shiftBO.getStartBy(),new Date(),null,null));

        }
        return 1;
    }

    public ShiftBO findOneShiftBO(String id) {
        ShiftPO shiftPO = this.baseMapper.selectOne(Wrappers.lambdaQuery(ShiftPO.class).eq(ShiftPO::getId, id));
        ShiftBO shiftBO = new ShiftBO(shiftPO.getId(), shiftPO.getShiftName(), shiftPO.getShiftHead(), shiftPO.getCommuteTime(), shiftPO.getShiftLate(), shiftPO.getShiftLateEnd(), shiftPO.getShiftBoolean(), shiftPO.getShiftMaxLate(), shiftPO.getStartTime(), shiftPO.getStartBy(), shiftPO.getEndTime(), shiftPO.getEndBy(), null);
        System.out.println("dddddddddddddddddddddddddddddddddddddddddddddd");
        List<CommuteScheduleBO> list = new ArrayList<>();
        for (String commuteId : shiftBO.getCommuteTime().split(",")) {
            CommuteSchedulePO commuteSchedulePO = commuteScheduleMapper.selectOne(Wrappers.lambdaQuery(CommuteSchedulePO.class).eq(CommuteSchedulePO::getId, commuteId));
            list.add(new CommuteScheduleBO(commuteSchedulePO.getId(),commuteSchedulePO.getStartDay(),commuteSchedulePO.getStartTime(),commuteSchedulePO.getStartCart(),commuteSchedulePO.getEndDay(),commuteSchedulePO.getEndTime(),commuteSchedulePO.getEndCart(),commuteSchedulePO.getMaxInAdvance(),commuteSchedulePO.getMaxLaterLate(),commuteSchedulePO.getMaxLaterDay(),commuteSchedulePO.getMinInAdvance(),commuteSchedulePO.getMinLaterLate(),commuteSchedulePO.getMinLaterDay()));
        }
        shiftBO.setCommuteScheduleBO(list);
        return shiftBO;
    }
}
